home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dirut / rh_ls.zip / LS.C < prev    next >
Text File  |  1991-08-30  |  29KB  |  787 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /* ls - a Unix-like directory listing program for MS-DOS 2.x +               */
  4. /*                                                                           */
  5. /*               1984  R. Edward Nather                                      */
  6. /*               1985  Larry A. Shurr                                        */
  7. /*               1989  R. Ray Hensel  Comverted to Microsoft V.5             */
  8. /*                                    cleaned up code                        */
  9. /*                                    added many coments                     */
  10. /*                                    replaced heap sort with quick sort     */
  11. /*                                                                           */
  12. /*****************************************************************************/
  13. #include <stdio.h>
  14. #include <dos.h>
  15.  
  16. char*     malloc();                     /*Assure that malloc() is char* type*/
  17.  
  18. typedef int bool;                       /*Define "boolean" type*/
  19. typedef int mchar;                      /*Define "metachar" type*/
  20.  
  21. #define FALSE 0                         /*Define "false" value*/
  22. #define TRUE  1                         /*Define "true"  value*/
  23. #define is ==                           /*Define some readable stuff*/
  24. #define isnot !=
  25. #define and &&
  26. #define or ||
  27. /*****************************************************************************/
  28.                /* customizing constants */
  29.  
  30. #define NAMESIZ 13                      /*12 character name + NULL*/
  31. #define ONECS   512                     /*cluster size on one-sided floppy*/
  32. #define TWOCS   1024                    /*cluster size on two-sided floppy*/
  33. #define HARDCS  4096                    /*cluster size on hard disk*/
  34. #define SCRSIZ  22                      /*scrolling size of display screen*/
  35. /*****************************************************************************/
  36. struct dta {                            /*DOS Disk Transfer Address table*/
  37.   char reserved[21];                    /*used in "find next" operation*/
  38.   char attr;                            /*file attribute byte*/
  39.   unsigned ftime;                       /*time of last modification*/
  40.   unsigned fdate;                       /*date of last modification*/
  41.   long fsize;                           /*file size in bytes*/
  42.   char fname[NAMESIZ];                  /*filename and extension*/
  43. };
  44. typedef struct dta DTA;
  45. /*****************************************************************************/
  46. struct outbuf {                         /*output buffer -- array of file info*/
  47.   unsigned oattr;                       /*file attributes*/
  48.   unsigned odate;                       /*file creation/modification date*/
  49.   unsigned otime;                       /*file creation/modification time*/
  50.   long     osize;                       /*file size in bytes*/
  51.   char     oname[NAMESIZ+1];            /*file name*/
  52. } *obuf;
  53. typedef struct outbuf OUTBUF;
  54. /*****************************************************************************/
  55. char spath[80];                         /*holds current pathname string*/
  56.  
  57. mchar qs       = '\\';                  /*filename separator character*/
  58. char  dqs[]    = "\\";                  /*filename separator (string)*/
  59.                                                    
  60. bool  df_all   = FALSE,                 /*1 => show hidden files by default*/
  61.       df_colm  = FALSE,                 /*1 => 1-column listing by default*/
  62.       df_du    = FALSE,                 /*1 => include disk use by default*/
  63.       df_id    = FALSE,                 /*1 => always identify directory*/
  64.       df_long  = FALSE,                 /*1 => long listing by default*/
  65.       df_more  = FALSE,                 /*1 => pause for more*/
  66.       df_rsort = FALSE,                 /*1 => reverse sort by default*/
  67.       df_tsort = FALSE,                 /*1 => time sort by default*/
  68.       f_help   = FALSE                  /*Help screen requested*/
  69.       ;
  70.                 /* global variables and flags */
  71.  
  72. int   f_all,                            /*Include hidden & system files*/
  73.       f_colm,                           /*1-column format*/
  74.       f_du,                             /*Print disk usage*/
  75.       f_long,                           /*Long listing*/
  76.       f_rsort,                          /*Reverse sort*/
  77.       f_tsort,                          /*Timesort the listing*/
  78.       f_more  = FALSE,                  /*Pause for more*/
  79.       f_recd  = FALSE,                  /*Recursive descent requested*/
  80.       f_so    = FALSE,                  /*Print sizes only*/
  81.       f_tsc   = FALSE,                  /*Output is to console screen*/
  82.       np,                               /*number of groups printed*/
  83.       nargs,                            /*number of non-option arguments*/
  84.       clsize  = 0,                      /*size of a cluster, in bytes*/
  85.       clmask,                           /*clsize-1 for rounding & chopping*/
  86.       drive                             /*code number for drive requested*/
  87.       ;
  88.       
  89. long  left,                             /*unused space left on disk*/
  90.       total                             /*total of sizes encountered*/
  91.       ;
  92. /*****************************************************************************/
  93. main(argc, argv)
  94. int  argc;
  95. char *argv[];
  96. {
  97. char  *s
  98.         ;
  99. int  c = 0,
  100.      nt = 0
  101.      ;
  102.  
  103.   setbuf(stdout,malloc(BUFSIZ));        /*Force standard output buffered*/
  104.  
  105.   df_all   = envbool("LSALL",df_all);   /*1 => show hidden files by default*/
  106.   df_colm  = envbool("LSCOLM",df_colm); /*1 => 1-column listing by default*/
  107.   df_du    = envbool("LSDU",df_du);     /*1 => include disk use by default*/
  108.   df_id    = envbool("LSID",df_id);     /*1 => always identify directory*/
  109.   df_long  = envbool("LSLONG",df_long); /*1 => long listing by default*/
  110.   df_more  = envbool("LSMORE",df_more); /*1 => pause for more*/
  111.   df_rsort = envbool("LSRSORT",df_rsort);/*1 => reverse sort by default*/
  112.   df_tsort = envbool("LSTSORT",df_tsort);/*1 => time sort by default*/
  113.  
  114.   qs = envchar("LSQS",qs);              /*Get default directory separator*/
  115.   dqs[0] = qs;
  116.  
  117.   f_all   = df_all;                     /*include hidden & system files*/
  118.   f_colm  = df_colm;                    /*1-column format*/
  119.   f_du    = df_du;                      /*print disk usage*/
  120.   f_long  = df_long;                    /*long listing*/
  121.   f_more  = df_more;                    /*pause for more*/
  122.   f_rsort = df_rsort;                   /*reverse sort*/
  123.   f_tsort = df_tsort;                   /*timesort the listing*/
  124.  
  125.   f_recd  = FALSE;                      /*recursive descent requested*/
  126.   f_so    = FALSE;                      /*print sizes only*/
  127.  
  128.                /* process input options */
  129.  
  130.     while(--argc > 0 and (*++argv)[0] is '-') {
  131.           for(s = argv[0]+1; *s isnot '\0'; s++) {
  132.               switch(*s) {
  133.                 case 'a':               /*-a: list all files*/
  134.                       f_all   = !f_all;
  135.                       break;                    
  136.                 case 'c':               /*-c: 1-column listing requested*/
  137.                       f_colm  = !f_colm;
  138.                       break;
  139.                 case 'h':               /*-h: print help and quit*/
  140.                       f_help  = TRUE;
  141.                       break;
  142.                 case 'l':               /*-l: long listing requested*/
  143.                       f_long  = !f_long;
  144.                       break;
  145.                 case 'm':               /*-m: pause for more*/
  146.                       f_more  = TRUE;
  147.                       break;
  148.                 case 'r':               /*-r: reverse sort direction*/
  149.                       f_rsort = !f_rsort;
  150.                       break;
  151.                 case 's':               /*-s: print sizes only*/
  152.                       f_so = TRUE;
  153.                       if (*(s+1) is '1') {
  154.                         clsize = ONECS; /*diskuse for 1-sided floppy*/
  155.                         s++;
  156.                         nt++;
  157.                       }
  158.                       else if (*(s+1) is '2') {
  159.                         clsize = TWOCS; /*or 2-sided*/
  160.                             s++;
  161.                         nt++;
  162.